package org.jeecg.modules.user.version.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.system.upgrade.entity.BikeUpgradeApp;
import org.jeecg.modules.system.upgrade.entity.BikeUpgradeAppPackage;
import org.jeecg.modules.system.upgrade.entity.BikeUpgradeAppUpgrade;
import org.jeecg.modules.system.upgrade.entity.BikeUpgradeAppVersion;
import org.jeecg.modules.system.upgrade.mapper.BikeUpgradeAppMapper;
import org.jeecg.modules.system.upgrade.mapper.BikeUpgradeAppPackageMapper;
import org.jeecg.modules.system.upgrade.mapper.BikeUpgradeAppUpgradeMapper;
import org.jeecg.modules.system.upgrade.mapper.BikeUpgradeAppVersionMapper;
import org.jeecg.modules.user.version.service.VersionService;
import org.jeecg.modules.utils.BikeResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Service
public class VersionServiceImpl implements VersionService {

    @Autowired
    private BikeUpgradeAppVersionMapper bikeUpgradeAppVersionMapper;
    @Autowired
    private BikeUpgradeAppPackageMapper bikeUpgradeAppPackageMapper;
    @Autowired
    private BikeUpgradeAppMapper bikeUpgradeAppMapper;
    @Autowired
    private BikeUpgradeAppUpgradeMapper bikeUpgradeAppUpgradeMapper;

    @Value("${application.domain}")
    public String domain;

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    /**
     * 获取app最新版本
     * @param appId
     * @return
     */
    @Override
    public BikeResult getNewVersion(String appId) {
        JSONObject jsonObject = new JSONObject();

        BikeUpgradeApp bikeUpgradeApp = bikeUpgradeAppMapper.selectById(appId);


        BikeUpgradeAppVersion bikeUpgradeAppVersion = bikeUpgradeAppVersionMapper.selectById(bikeUpgradeApp.getVersionId());
        if (bikeUpgradeAppVersion!=null){
            BikeUpgradeAppPackage bikeUpgradeAppPackage = bikeUpgradeAppPackageMapper.selectOne(new QueryWrapper<BikeUpgradeAppPackage>().eq("version_id", bikeUpgradeApp.getVersionId()));

            if (bikeUpgradeAppPackage==null){
                return BikeResult.error("0","当前为最新版本",null);
            }
            // 1 为静默 .wgt 0为 整包     .ipa.apk
            String isSilent = BikeConstant.y_n_no;
            //对文文件的全名进行截取然后在后缀名进行删选。
            int begin = Objects.requireNonNull(bikeUpgradeAppPackage.getFileName()).lastIndexOf(".");
            int last = bikeUpgradeAppPackage.getFileName().length();
            //获得文件后缀名
            if (begin<0){
                begin=0;
            }
            String fileSuffix = bikeUpgradeAppPackage.getFileName().substring(begin, last);
            if (fileSuffix.equals(".wgt")){
                isSilent= BikeConstant.y_n_yes;
            }
            jsonObject.put("isSilent",isSilent);
            jsonObject.put("fileName",bikeUpgradeAppPackage.getFileName());
            jsonObject.put("version",bikeUpgradeAppVersion.getVersion());
            jsonObject.put("content",bikeUpgradeAppVersion.getMemo());
            jsonObject.put("versionId",bikeUpgradeAppVersion.getId());
            jsonObject.put("isForce",bikeUpgradeAppVersion.getIsForce());
            jsonObject.put("downloadUrl",domain+bikeUpgradeAppPackage.getDownloadUrl());
            return BikeResult.ok("1","成功",jsonObject);
        }

        return BikeResult.error("0","当前为最新版本",null);
    }


    /**
     * 下载日志
     * @param bikeUpgradeAppUpgrade
     * @return
     */
    @Override
    public BikeResult insertUpgrade(BikeUpgradeAppUpgrade bikeUpgradeAppUpgrade) {

        bikeUpgradeAppVersionMapper.updateDownloadCount(bikeUpgradeAppUpgrade.getVersionId());

        bikeUpgradeAppUpgrade.setUpgradeTime(new Date());
        int insert = bikeUpgradeAppUpgradeMapper.insert(bikeUpgradeAppUpgrade);


        return BikeResult.ok("1","下载成功",insert);
    }
}
